package com.dhy.generator;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class StaticGenerator {
    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
//        System.out.println(projectPath);
        String inputPath = projectPath + File.separator + "demo-project" + File.separator + "acm-template";
        String outputPath = projectPath + File.separator + "out";
//        copyFilesByHutool(inputPath, outputPath);
        copyFilesByRecursive(inputPath, outputPath);
    }

    //用第三方库
    public static void copyFilesByHutool(String inputPath, String outputPath){
        FileUtil.copy(inputPath, outputPath, false);
    }

    //用递归实现
    public static void copyFilesByRecursive(String inputPath, String outputPath){
        File inputFile = new File(inputPath);
        File outputFile = new File(outputPath);
        try{
            copyFilesByRecursive(inputFile, outputFile);
        }catch (Exception e){
            System.err.println("文件复制失败");
            e.printStackTrace();
        }
    }
    private static void copyFilesByRecursive(File inputFile, File outputFile) throws IOException {
        if(inputFile.isDirectory()){
            //创建目录才能复制
            File destOutputFile = new File(outputFile,inputFile.getName());
            if(!destOutputFile.exists()){
                destOutputFile.mkdirs();
            }

            File[] files = inputFile.listFiles();
            for(File file : files){
                copyFilesByRecursive(file, destOutputFile);
            }
        }else{
//            FileUtil.copy(inputFile, outputFile, false);
            Path destPath = outputFile.toPath().resolve(inputFile.getName());
            Files.copy(inputFile.toPath(),destPath, StandardCopyOption.REPLACE_EXISTING);
        }
    }
}
